// pages/me/info/info.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unList:[],
    index:0,
    memo:null,
    selectCommunityNull: null,
    selectHouseNull: null,//设置的变量
    rawData: [],
    houseListArray: [
      [], [], []
    ],
    multiIndex: [0, 0, 0],
    selectedHouseId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    app.sendGetRequest('/system/community/list/all',(res)=>{
      console.log("小区列表", res.rows)
      that.setData({
        unList:res.rows
      })
    },(err)=>{
      console.log(err)
    })
  },
  RegisterInfo:function(e){
    console.log(e)
    console.log(this.data.index)
    var data =  {
      communityId:this.data.unList[this.data.index].communityId,
      deptId: this.data.selectedHouseId,
      remarks:this.data.memo
    }
    console.log(data)
    app.sendPostRequest('/system/apply',data,(res)=>{
      console.log(res)
      wx.switchTab({
        url: '../../index/index',
      })
    },(err)=>{
      console.log(err)
    })
  },
  
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      selectCommunityNull: 0
    })

    // 查询小区下的所有房屋信息
    this.getHouseList(this.data.unList[this.data.index].communityId)
  },
  
  // 获取选中小区的房屋信息
  getHouseList(communityId) {
    // communityId = 1
    var that = this
    that.setData({
      houseListArray: [[],[],[]],
      multiIndex: [0, 0, 0],
      selectHouseNull: null
    })
    app.sendGetRequest('/system/dept/list/' + communityId, (res)=>{
      var houseListArray = that.handleHouseList(res.data)
      that.setData({
        rawData: houseListArray,
        'houseListArray[0]': houseListArray,
        'houseListArray[1]': houseListArray[0].children,
        'houseListArray[2]': houseListArray[0].children[0].children,
      })
    },(err)=>{
      console.log(err)
    })
  },

  // 封装房屋列表成树形结构
  handleHouseList(data) {
    var childrenListMap = {}
    var nodeIds = {}
    var tree = []

    for (const d of data) {
      const parentId = d['parentId']
      if (childrenListMap[parentId] == null) {
        childrenListMap[parentId] = []
      }
      nodeIds[d['deptId']] = d
      childrenListMap[parentId].push(d)
    }

    for (const d of data) {
      const parentId = d['parentId']
      if (nodeIds[parentId] == null) {
        tree.push(d)
      }
    }

    for (const t of tree) {
      adaptToChildrenList(t)
    }

    function adaptToChildrenList(o) {
      if (childrenListMap[o['deptId']] !== null) {
        o['children'] = childrenListMap[o['deptId']]
      }
      if (o['children']) {
        for (const c of o['children']) {
          adaptToChildrenList(c)
        }
      }
    }
    return tree
  },

  // 选中的数据发生变化
  bindHousePickerColumnChange(e) {
    console.log('修改的列值为', e.detail.column, '，值为', e.detail.value);
    const [...multiIndex] = this.data.multiIndex;
    multiIndex[e.detail.column] = e.detail.value;
    if (e.detail.column === 0) {
      multiIndex[1] = 0;
      multiIndex[2] = 0;
    }
    if (e.detail.column === 1) {
      multiIndex[2] = 0;
    }
    const children = this.data.rawData[multiIndex[0]].children;
    this.setData({
      multiIndex
    });
    if(children != undefined) {
      this.setData({
        'houseListArray[1]': children
      });
      if(children[multiIndex[1]].children != undefined) {
        this.setData({
          'houseListArray[2]': children[multiIndex[1]].children
        });
      } else {
        this.setData({
          'houseListArray[2]': []
        });
      }
    } else {
      this.setData({
        'houseListArray[1]': [],
        'houseListArray[2]': [],
      });
    }
  },

  // 选择器选中房屋
  bindHousePickerChange(e){
    this.setData({
      selectedHouseId: this.data.rawData[e.detail.value[0]].children[e.detail.value[1]].children[e.detail.value[2]].deptId,
      multiIndex: e.detail.value,
      selectHouseNull: 0
    })
  }
})